package com.cris.controller;

import com.cris.helper.MapHelper;
import com.cris.model.Product;
import com.cris.model.ProductType;
import com.cris.service.ProductService;
import com.cris.service.ProductTypeService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by owen on 2017/4/6.
 */
@Controller
public class ProductController {
    private static final Log logger = LogFactory.getLog(ProductController.class);

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductTypeService productTypeService;


    //进入主页
    @RequestMapping(value = "/main",method = RequestMethod.GET)
    public String main(){
        return "main";
    }

    //进入产品管理
    @RequestMapping(value = "/product",method = RequestMethod.GET)
    public String product(){
        return "product";
    }

    //显示产品
    @RequestMapping(value="/product_show")
    public String showProduct(Model model){
        List<Product> productList=productService.getProductList();
        List<ProductType> productTypeList = productTypeService.getParentProductTypeList();
        model.addAttribute("productTypeList",productTypeList);
        model.addAttribute("productList",productList);
        return "product_show";
    }

    //进入创建产品界面
    @RequestMapping(value = "/product_create",method = RequestMethod.GET)
    public String showCreate(Model model){
        List<ProductType> productTypeList = productTypeService.getChildProductTypeList();
        model.addAttribute("product",new Product());
        model.addAttribute("productTypeList",productTypeList);
        return "product_create";
    }
    //创建产品
    @RequestMapping(value="/product_create",method = RequestMethod.POST)
    public String doCreate(@ModelAttribute Product product){
        HashMap<String,Object> map = new HashMap<String,Object>();
        map = MapHelper.put(product);
        productService.addProduct(map);
        return "redirect:/product_show";
    }
    //删除产品
    @RequestMapping(value = "/product_delete",method = RequestMethod.GET)
    public String deleteProduct(@RequestParam long id, Model model){
        productService.deleteProduct(id);
        return "redirect:/product_show";
    }
    //进入编辑产品界面
    @RequestMapping(value = "/product_edit",method = RequestMethod.GET)
    public String showEdit(@RequestParam long id,Model model){
        List<ProductType> productTypeList = productTypeService.getChildProductTypeListById(id);
        Product product = productService.getProduct(id);
        model.addAttribute("productTypeList",productTypeList);
        model.addAttribute("product",product);
        return "product_edit";
    }
    //编辑产品
    @RequestMapping(value = "/product_edit",method = RequestMethod.POST)
    public String doEdit(@ModelAttribute Product product,@RequestParam long id){
        Map<String,Object> map;
        map=MapHelper.put(product);
        productService.updateProduct(id,map);
        return "redirect:/product_show";

    }
    //搜索产品
    @RequestMapping(value = "/product_search",method = RequestMethod.GET)
    public String Search(Model model){
        List<ProductType> productTypeList=productTypeService.getChildProductTypeList();
        model.addAttribute("productTypeList",productTypeList);
        return "product_search";
    }
    @RequestMapping(value = "/product_search",method = RequestMethod.POST)
    public String productSearch(@RequestParam String name,Model model){
        List<Product> productList=productService.getProductByFuzzySearch(name);
        model.addAttribute("productList",productList);
        return "product_search";
    }
    //按类型显示产品
    @RequestMapping(value = "/product_showByType",method = RequestMethod.GET)
    public String showProductByType(@RequestParam long typeid,Model model){
        List<Product> productList=productService.getProductByTypeId(typeid);
        List<ProductType> productTypeList = productTypeService.getParentProductTypeList();
        model.addAttribute("productTypeList",productTypeList);
        model.addAttribute("productList",productList);
        return "product_show";
    }

}
